package servlets;

import beans.MaintenanceBean;
import beans.Vehicle;
import beans.Drivers;
import db.MaintenanceDAO;
import db.VehicleDao;
import db.DriversDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.List;

@WebServlet(name = "MaintenanceServlet", value = "/MaintenanceServlet")
@MultipartConfig
public class MaintenanceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("d".equals(action)) { // 维修单提交页面
            try {
                VehicleDao vehicleDAO = new VehicleDao();
                DriversDao driversDAO = new DriversDao();

                // 获取所有可用车辆和司机
                List<Vehicle> vehicles = vehicleDAO.getAllVehicle(); // 确保这个方法存在
                List<Drivers> drivers = driversDAO.getAllDriver(); // 确保这个方法存在

                // 设置请求属性
                request.setAttribute("vehicles", vehicles);
                request.setAttribute("drivers", drivers);
                System.out.println("查询到的车辆数量: " + vehicles.size());
                System.out.println("查询到的司机数量: " + drivers.size());

                // 转发到JSP
                request.getRequestDispatcher(request.getContextPath() + "/pirt/drivers/Repair_Orders.jsp");
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServletException(e);
            }
        }
        // 其他action处理...
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("submit".equals(action)) {
            try {
                // 获取表单数据
                int vehicleID = Integer.parseInt(request.getParameter("vehicleID"));
                int driverID = Integer.parseInt(request.getParameter("driverID"));
                Date maintenanceTime = Date.valueOf(request.getParameter("maintenanceTime"));
                BigDecimal cost = new BigDecimal(request.getParameter("cost"));
                String purpose = request.getParameter("purpose");
                Part filePart = request.getPart("receipt");

                // 读取文件内容
                byte[] receipt = filePart.getInputStream().readAllBytes();

                // 创建维修单对象
                MaintenanceBean maintenance = new MaintenanceBean();
                maintenance.setVehicleID(vehicleID);
                maintenance.setDriverID(driverID);
                maintenance.setMaintenanceTime(maintenanceTime);
                maintenance.setCost(cost);
                maintenance.setPurpose(purpose);
                maintenance.setReceipt(receipt);
                maintenance.setStatus("待审核");

                // 保存到数据库
                MaintenanceDAO maintenanceDAO = new MaintenanceDAO();
                boolean success = maintenanceDAO.addMaintenance(maintenance);

                if (success) {
                    request.setAttribute("message", "维修单提交成功！");
                } else {
                    request.setAttribute("error", "维修单提交失败，请重试！");
                }

                // 重新加载页面
                doGet(request, response);

            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("error", "提交过程中发生错误: " + e.getMessage());
                doGet(request, response);
            }
        }
    }
}
